package egovframework.npqs.pqi.mobile.jsonparser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;
import egovframework.npqs.pqi.mobile.AppStatus;
import egovframework.npqs.pqi.mobile.common.Const;
import egovframework.npqs.pqi.mobile.jsoncontent.ErrorItem;
import egovframework.npqs.pqi.mobile.jsoncontent.ProdResultItem;
import egovframework.npqs.pqi.mobile.util.BrUtilManager;
import egovframework.npqs.pqi.mobile.util.ParameterUtil;

public class ProdResultThread extends BasicThread {

	public interface ProdResultThreadCallback {
		public void OnProdResultCompleted(boolean isNetworkConntected, boolean results
				, String message);
		public void OnProdResultError(ErrorItem errorItem);
	}

	private String url;
	private ProdResultThreadCallback callback;
	private HashMap<String, String> searchMap;
	private ArrayList<NameValuePair> post;
	
	/* 파라메터*/
	
	public ProdResultThread(Context context,
			ProdResultThreadCallback callback, HashMap<String, String> searchMap
			, String vpnCnId) {
		super(vpnCnId);
		this.context = context;
		this.callback = callback;
		this.appStatus = (AppStatus) context.getApplicationContext();
		this.searchMap = searchMap;
	}

	@Override
	public void run() {
		Map<String,String> map = new HashMap<String, String>();
		map.put(Const.SEARCHCODE, searchMap.get("code").toString());
		map.put(Const.SEARCHSDATE, searchMap.get("sDate").toString());
		map.put(Const.SEARCHEDATE, searchMap.get("eDate").toString());
		String param = "";
		param = parameterReturn(Const.REQ019, (HashMap<String, String>) map);
		
		serviceBroker(param, context);
	}

	@Override
	public void settingObject() {
		// 통신불통상태이면
		if(!appStatus.utilm.isNetworkConnected(context)) {
			callback.OnProdResultCompleted(false, false, "통신 에러");
			return;
		}
		try {
			json = new JSONObject(returnJson);
			if(json == null) {	// json 값이 null 일때
				callback.OnProdResultError(null);
				return;
			}
			int result = json.getInt(Const.RESULT);
			
			ArrayList<ProdResultItem>
				arrProdItem = new ArrayList<ProdResultItem>();
			
			if(result == 0){	// result 값이 0일 경우
				String data = json.getString(Const.DATA);
				
				ErrorItem errorItem = new ErrorItem();
				errorItem.setrDesc(data);

				appStatus.setArrProdResultItem(arrProdItem);
				callback.OnProdResultError(errorItem);
			}else{	// result 값이 0이 아닐 경우
				JSONArray arrayLogin = json.getJSONArray(Const.DATA);
				ProdResultItem prodItem = null;
				for (int i = 0; i < arrayLogin.length(); i++) {
					JSONObject prod = arrayLogin.getJSONObject(i);
					
					prodItem = new ProdResultItem();
					prodItem.setFieDe(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, ProdResultItem.FIEDE)));
					prodItem.setAllInsttNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, ProdResultItem.ALLINSTTNM)));
					prodItem.setFullRecNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, ProdResultItem.FULLRECNO)));
					prodItem.setResOffCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, ProdResultItem.RESOFFCD)));
					prodItem.setResNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, ProdResultItem.RESNO)));
					prodItem.setResSeq(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, ProdResultItem.RESSEQ)));
					prodItem.setVenNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, ProdResultItem.VENNM)));
					prodItem.setPrdlstNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, ProdResultItem.PRDLSTNM)));
					prodItem.setProgrsNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, ProdResultItem.PROGRSNM)));
					prodItem.setDspsTrgetNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, ProdResultItem.DSPSTRGETNM)));
					prodItem.setNatNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, ProdResultItem.NATNM)));
					prodItem.setLabCnt(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, ProdResultItem.LABCNT)));
					prodItem.setDbyhsCnt(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, ProdResultItem.DBYHSCNT)));
					prodItem.setDsnfCnt(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, ProdResultItem.DSNFCNT)));
					//prodItem.setTotalRows(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, ProdResultItem.TOTALROWS)));
					
					arrProdItem.add(prodItem);
				}
				appStatus.setArrProdResultItem(arrProdItem);
				callback.OnProdResultCompleted(true, true, mResult);
			}
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		/*JSONParser jParser = new JSONParser();

		url = Const.MAIN_URL;
		post = ParameterUtil.getPostProdResult(searchMap);
		Log.i("post","post = " + post);
		
		JSONObject json = jParser.getJSONFromUrl(url, post);*/
		

		/*try {		
			
			
		} catch (JSONException e) {
			
		}*/
	}
}
